package tw.cuttle;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tw.cuttle.bean.Action;
import tw.cuttle.bean.Process;
public class ActionHelper {
    private HttpServletRequest request;
    private HttpServletResponse response;
    private ServletContext context;
    private Boolean isAjaxRequest;
    private List<Action> executedActions=new ArrayList<Action>();
    private List<Process> executedProcesses=new ArrayList<Process>();
    private Exception currentException;
    private Object form;
    public Exception getCurrentException() {
		return currentException;
	}
	public void setCurrentException(Exception currentException) {
		this.currentException = currentException;
	}
	/**
     * @return the form
     */
    public Object getForm() {
        return form;
    }
    /**
     * @param form the form to set
     */
    public void setForm(Object form) {
        this.form = form;
    }
    /**
     * @return the executedProcesses
     */
    public List<Process> getExecutedProcesses() {
        return executedProcesses;
    }
    /**
     * @param executedProcesses the executedProcesses to set
     */
    public void setExecutedProcesses(List<Process> executedProcesses) {
        this.executedProcesses = executedProcesses;
    }
    /**
     * @return the executedActions
     */
    public List<Action> getExecutedActions() {
        return executedActions;
    }
    /**
     * @param executedActions the executedActions to set
     */
    public void setExecutedActions(List<Action> executedActions) {
        this.executedActions = executedActions;
    }
    /**
     * @return the isAjaxRequest
     */
    public Boolean getIsAjaxRequest() {
        return isAjaxRequest;
    }
    /**
     * @param isAjaxRequest the isAjaxRequest to set
     */
    public void setIsAjaxRequest(Boolean isAjaxRequest) {
        this.isAjaxRequest = isAjaxRequest;
    }
    public void actionForward(String actionId){
	CuttleContext.getActionContext().getActionHelper().getExecutedActions().add(CuttleContext.getBeanHolder().getAction(actionId));
	if(isAjaxRequest){
	    CuttleContext.getActionHandler().ajaxRequest(actionId);
	}else{
	    CuttleContext.getActionHandler().request(actionId);
	}
    }
    /**
     * @return the context
     */
    public ServletContext getContext() {
        return context;
    }
    /**
     * @param context the context to set
     */
    public void setContext(ServletContext context) {
        this.context = context;
    }
    /**
     * @return the request
     */
    public HttpServletRequest getRequest() {
        return request;
    }
    /**
     * @param request the request to set
     */
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }
    /**
     * @return the response
     */
    public HttpServletResponse getResponse() {
        return response;
    }
    /**
     * @param response the response to set
     */
    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }
    public Action getCurrentAction(){
    	System.out.println("executedActions:"+executedActions);
	if(executedActions==null || executedActions.size()==0)return null;
	return executedActions.get(executedActions.size()-1);
    }
    public Process getCurrentProcess(){
	if(executedProcesses==null || executedProcesses.size()==0)return null;
	return executedProcesses.get(executedProcesses.size()-1);
    }
    
}
